(function (exports) {

    var $ = null;

    var bgWidthArr = [512, 456, 430];

    var HomeLayer = cc.Layer.extend({

        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onCCSLoadFinish: function () {
            var that = this;
        },

        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
        },
        ctor: function () {
            this._super();

            var that = this;

            loadCCSTo(res.HomeLayer_json, this, "Layer");
            $ = create$(this.getChildByName("Layer"));
            this.shipei();

            this.initHead();
            this.initUI();
            this.initSpine();
        },
        shipei: function(){
            if(isIpad()){
                $('top').setPositionY(cc.winSize.height);
                for(var i=1;i<=4;i++){
                    $('root.cityworld_bg1_' + i).setPositionX((i-1)*bgWidthArr[0]*1.25);
                    $('root.cityworld_bg1_' + i).setScale(1.25);

                    $('root.cityworld_bg2_' + i).setPositionX((i-1)*bgWidthArr[1]*1.25);
                    $('root.cityworld_bg2_' + i).setScale(1.25);

                    $('root.cityworld_bg3_' + i).setPositionX((i-1)*bgWidthArr[2]*1.25);
                    $('root.cityworld_bg3_' + i).setScale(1.25);
                }
            }
        },
        initHead: function(){
            TouchUtils.setOnclickListener($('top.yuanbaobg.add'), function (node) {

            });
            TouchUtils.setOnclickListener($('top.tilibg.add'), function (node) {

            });
            TouchUtils.setOnclickListener($('top.headbg'), function (node) {

            });
            $('top.headbg.head').setTexture('res/image/avatar/avatar_1.jpg');
        },
        initSpine: function(){
            var that = this;
            //抽签
            TouchUtils.setOnclickListener($('root.cityworld_bg1_1.dizuo1'), function (node) {
                that.addChild(new LotteryLayer());
            });
            //签到
            TouchUtils.setOnclickListener($('root.cityworld_bg1_2.dizuo2'), function (node) {
                that.addChild(new DailyLayer());
            });
            //飞行棋
            TouchUtils.setOnclickListener($('root.cityworld_bg1_2.dizuo1'), function (node) {
                that.addChild(new FlyingChessLayer());
            });
            //战斗
            TouchUtils.setOnclickListener($('root.cityworld_bg1_3.dizuo'), function (node) {
                // that.addChild(new FightLayer());
                cc.director.pushScene(new FightScene());
            });
            //选角色
            TouchUtils.setOnclickListener($('root.cityworld_bg1_2.dizuo'), function (node) {
                that.addChild(new ChooseRoleLayer());
            });
            //背单词
            TouchUtils.setOnclickListener($('root.cityworld_bg1_3.dizuo1'), function (node) {
                that.addChild(new LearnWordLayer());
            });

            if(!cc.sys.isNative)  return;

            //  1_0 1_1 2_0 2_1 3_0 3_1
            // var guide = new sp.SkeletonAnimation('res/image/guide/duihua.json', 'res/image/guide/duihua.atlas');
            // guide.setPosition(cc.p(400, 200));
            // this.addChild(guide);
            // guide.addAnimation(0, '2_0', false);

            //--------------------------------------------bg1
            //鲜花
            var xianhuaspine = new sp.SkeletonAnimation('res/image/home/tj.json', 'res/image/home/tj.atlas');
            xianhuaspine.setPosition(cc.p(50, 100));
            $('root.cityworld_bg1_1.dizuo').addChild(xianhuaspine);
            xianhuaspine.addAnimation(0, 'idle', true);
            //抽签 idle
            var chouqianspine = new sp.SkeletonAnimation('res/image/home/abj.json', 'res/image/home/abj.atlas');
            chouqianspine.setPosition(cc.p(50, 0));
            $('root.cityworld_bg1_1.dizuo1').addChild(chouqianspine);
            chouqianspine.addAnimation(0, 'idle', true);

            //--------------------------------------------bg2
            //邮箱  atk idle
            var mailspine = new sp.SkeletonAnimation('res/image/home/yx.json', 'res/image/home/yx.atlas');
            mailspine.setPosition(cc.p(50, 0));
            $('root.cityworld_bg1_2.dizuo2').addChild(mailspine);
            mailspine.addAnimation(0, 'atk', true);
            //庙 atk idle
            var miaospine = new sp.SkeletonAnimation('res/image/home/ck.json', 'res/image/home/ck.atlas');
            miaospine.setPosition(cc.p(50, 0));
            $('root.cityworld_bg1_2.dizuo1').addChild(miaospine);
            miaospine.addAnimation(0, 'atk', true);
            // //排行榜 atk idle
            // var phbspine = new sp.SkeletonAnimation('res/image/home/phb.json', 'res/image/home/phb.atlas');
            // phbspine.setPosition(cc.p(0, 0));
            // $('root.cityworld_bg1_2.dizuo').addChild(phbspine);
            // phbspine.addAnimation(0, 'idle', true);

            //--------------------------------------------bg3
            //熊猫 atk idle
            var xmspine = new sp.SkeletonAnimation('res/image/home/xm.json', 'res/image/home/xm.atlas');
            xmspine.setPosition(cc.p(50, 0));
            $('root.cityworld_bg1_3.dizuo1').addChild(xmspine);
            xmspine.addAnimation(0, 'atk', true);
            //商店 atk idle
            var shopspine = new sp.SkeletonAnimation('res/image/home/sr.json', 'res/image/home/sr.atlas');
            shopspine.setPosition(cc.p(50, 0));
            $('root.cityworld_bg1_3.dizuo').addChild(shopspine);
            shopspine.addAnimation(0, 'atk', true);


            //--------------------------------------------bg4
            //圣殿 atk idle
            var shengdianspine = new sp.SkeletonAnimation('res/image/home/shengdian.json', 'res/image/home/shengdian.atlas');
            shengdianspine.setPosition(cc.p(50, 0));
            $('root.cityworld_bg1_4.dizuo1').addChild(shengdianspine);
            shengdianspine.addAnimation(0, 'atk', true);
            //铸造 atk idle
            var tjpspine = new sp.SkeletonAnimation('res/image/home/tjp.json', 'res/image/home/tjp.atlas');
            tjpspine.setPosition(cc.p(50, 0));
            $('root.cityworld_bg1_4.dizuo').addChild(tjpspine);
            tjpspine.addAnimation(0, 'atk', true);
        },
        initUI: function(){
            var touchLayer = new cc.LayerColor(cc.color(0, 0, 0, 0), cc.winSize.width, cc.winSize.height);
            touchLayer.setName("touchLayer");
            touchLayer.setAnchorPoint(0, 0);
            this.addChild(touchLayer);

            var touchpos = null;
            var chupaiListener = cc.EventListener.create({
                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: false,
                onTouchBegan: function (touch, event) {
                    touchpos = touch.getLocation();
                    return true;
                },
                onTouchMoved: function (touch, event) {
                    var moveposx = touch.getLocation().x - touchpos.x;
                    touchpos = touch.getLocation();

                    if(isIpad()){
                        if ($('root.cityworld_bg1_1').getPositionX() + moveposx > 0
                            || $('root.cityworld_bg1_4').getPositionX() + moveposx < (1024 - bgWidthArr[0]*1.25)) {
                            return;
                        }
                    }else {
                        if ($('root.cityworld_bg1_1').getPositionX() + moveposx > 0
                            || $('root.cityworld_bg1_4').getPositionX() + moveposx < (1024 - bgWidthArr[0])) {
                            return;
                        }
                    }

                    for(var i=0;i<3;i++){
                        for(var j=0;j<4;j++){
                            var movextmp = moveposx;
                            if(i != 0)  movextmp = moveposx * bgWidthArr[i]/bgWidthArr[0] * 0.8;
                            var node = $('root.cityworld_bg' + (i + 1) + '_' + (j + 1));
                            node.setPositionX(node.getPositionX() + movextmp);
                        }
                    }
                },
                onTouchEnded: function (touch, event) {
                    touchpos = null;
                }
            });
            return cc.eventManager.addListener(chupaiListener, touchLayer);
        },
    });
    exports.HomeLayer = HomeLayer;
})(window);
